package org.example.controller;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.scheduledEntity.ScheduSpringCron;
import org.example.service.ScheduTaskService;
import org.example.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.ValidationException;
import java.util.List;

/**
 * @author 张在成
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/schedule")
public class ScheduTaskController {

    @Autowired
    private ScheduTaskService scheduTaskService;


    /**
     * 获取全部定时任务
     * @return
     */
    @GetMapping("/allTask")
    public R<?> getAllScheduleTask(){
        R<List<ScheduSpringCron>> scheduleTaskList = scheduTaskService.getScheduleTaskList();
        return  scheduleTaskList;
    }


    /**
     * 修改定时任务
     * @param springCron
     * @return
     */
    @PostMapping("/updateTask")
    public R<?> updateScheduleTask(@RequestBody ScheduSpringCron springCron) {
         try {
             boolean b = scheduTaskService.updateScheduleTask(springCron);
         }catch (ValidationException e){
             e.printStackTrace();
         }
        return  R.data(200,"修改成功");
    }

    /**
     * 新增定时任务
     * @param springCron
     * @return
     */
    @PostMapping("/insertTask")
    public R<?> insertScheduleTask(@RequestBody ScheduSpringCron springCron) {
        try {
            boolean b = scheduTaskService.insertScheduleTask(springCron);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  R.data(200,"修改成功");
    }




}
